<?php
/**
 * @version $Id: Letters.php 197 2008-03-01 17:23:36Z mikhail.miguel $
 * @author  Takeshi Kuriyama <kuri@keynext.co.jp>
 */
 

/**
 * Load PEAR error handling
 */
require_once 'PEAR.php';
  
/**
 * Load SpamCheck base class
 */

require_once dirname(dirname(dirname(dirname(__FILE__)))).'/Services/Trackback/SpamCheck.php';
   
class Services_Trackback_SpamCheck_Letters extends Services_Trackback_SpamCheck {

    // {{{ _options
    
    /**
     * Options for the Regex.
     *
     * @var array
     * @since 0.5.0
     * @access protected
     */
    var $_options = array(
        'continuous'    => false,
        'sources'       => array(),
        'elements'      => array(
            'title',
            'excerpt',
            'blog_name'
         ),
        'delimiter'     => '/',
        'modifier'     => 'x',
        'minlength'    => 1
    );

    // }}}
    // {{{ Services_Trackback_SpamCheck_Regex()

    /**
     * Constructor.
     * Create a new instance of the Regex spam protection module.
     *
     * @since 0.5.0
     * @access public
     * @param array $options An array of options for this spam protection module. General options are
     *                       'continuous':  Whether to continue checking more sources, if a match has been found.
     *                       'sources':     List of blacklist servers. Indexed.
     *                       'comparefunc': A compare function callback with parameters $haystack, $needle (like 'stripos').
     *                       'minmatches':  How many words have to be found to consider spam.
     * @return object(Services_Trackback_SpamCheck_WordList) The newly created SpamCheck object.
     */
    function Services_Trackback_SpamCheck_Letters($options = null)
    {
        if (is_array($options)) {
            foreach ($options as $key => $val) {
                $this->_options[$key] = $val;
            }
        }
    }
    
    // }}}
    // {{{ check()

    function check($trackback)
    {
        $length = mb_strlen($this->_checkSource($this->_options['sources'], $trackback));
        return ($length < $this->_options['minlength']);
    }

    // }}}
    // {{{ _checkSource()
    
    function _checkSource(&$sources, $trackback)
    {
        $myts =& MyTextSanitizer::getInstance();
        
        $del = $this->_options['delimiter'];
        $mod = $this->_options['modifier'];
        $spam = false;
        foreach ($this->_options['elements'] as $element) {
            $elements[$element] = $myts->stripSlashesGPC($trackback->get($element));
        }
        $matched = '';
        foreach ($elements as $element) {
            $encoding = 'ISO-8859-1';
            if(function_exists('mb_detect_encoding')){
                $encoding = mb_detect_encoding($element) ;
            }
            foreach($sources as $source) {            
                if (preg_match_all($del.$source.$del.$mod, d3blogBase::d3blog_encoding($element, $encoding), $matches)) {
                    $matched .= join("", $matches[1]);
                }
            }
        }
        return $matched;
    }

    // }}}
    
}